package com.hs.controller;


import com.hs.VO.LessonPO;
import com.hs.VO.LessonStudentVO;
import com.hs.config.PageObj;
import com.hs.config.R;
import com.hs.entity.Lesson;
import com.hs.entity.Student;
import com.hs.service.LessonService;
import com.hs.service.StudentService;
import com.hs.tool.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    LessonService lessonService;
    @RequestMapping("insert")
    @ResponseBody
    public R insert(Student student, Model model) {
        if (student == null) {
//            model.addAttribute("msg","请填写用户名密码等关键信息");
            return R.FAILURE("请填写用户名密码等关键信息", null);
        }
        if (StringTools.isEmpty(student.getName())) {
            model.addAttribute("msg", "请填写用户名");
            return R.FAILURE("请填写用户名", null);
        }
            if (StringTools.isEmpty(student.getNumber())) {
                model.addAttribute("msg", "请填写学号");
                return R.FAILURE("请填写学号", null);
            }
            if (StringTools.isEmpty(student.getPassWord())) {
                model.addAttribute("msg", "请填写密码");
                return R.FAILURE("请填写密码", null);
            }
            int count = studentService.selectByConditionCount(student);
            if (count > 0) {
                return R.FAILURE("用户已存在", null);
            }
            int i = studentService.insertSelective(student);
            if (i > 0) {
                return R.SUCCESS("", i);
            }
            return R.FAILURE("", 0);
        }

    /**
     * 加入班级
     * @param student
     * @param model
     * @param session
     * @return
     */
        @RequestMapping("joinClass")
        @ResponseBody
        public R joinClass(Student student, Model model, HttpSession session) {
            Student user = (Student) session.getAttribute("user");
            if (student == null) {
                return R.FAILURE("请填写用户名密码等关键信息", null);
            }
            if (null == student.getClassId()) {
                model.addAttribute("msg", "无班级主键！");
                return R.FAILURE("无班级主键！", null);
            }
            student.setId(user.getId());
            int i = studentService.updateCLassId(student);
            if (i > 0) {
                return R.SUCCESS("", i);
            }
            return R.FAILURE("", 0);
        }

        @RequestMapping("/myLearn")
        public String myLearn(HttpSession session, Model model, LessonPO record){
            Student student =(Student) session.getAttribute("user");
            session.setAttribute("user",student);
            model.addAttribute("user",student);
            if(record.getPageSize() == null){
                record.setPageSize(8);
            }
            if(record.getCurrentPage() == null ){
                record.setCurrentPage(0);
            }
            if(record.getFlag() == null){
                record.setFlag(0);
            }
            record.setCurrentPage(record.getCurrentPage() + record.getFlag());
            record.setPageStart(record.getCurrentPage() *  record.getPageSize());
            record.setUserId(student.getId());
            PageObj page = lessonService.studentLessonPage(record);
            model.addAttribute("page",page);
            model.addAttribute("num",page.getTotal());
            return "student/my_learn";
        }

        /**  课程详情
         *  id  = classLessonId
          * @param session
         * @param model
         * @param lesson
         * @return
         */
        @RequestMapping("/lessonProcessInfo")
        public String lessonProcessInfo(HttpSession session, Model model, LessonPO lesson){
            if(lesson == null || lesson.getId() == null){
                return "error";
            }
            LessonStudentVO lessonInfoVO = lessonService.selectStudentLessonInfo(lesson);
            session.setAttribute("user",session.getAttribute("user"));
            model.addAttribute("user",session.getAttribute("user"));
            model.addAttribute("lesson",lessonInfoVO);
            return "student/lesson_process_info";
        }

    /**
     * 参与课程
     * @param
     * @param model   classLessonIssueId   classLessonId
     * @param session
     * @return
     */
    @RequestMapping("handInLesson")
    @ResponseBody
    public R handInLesson(LessonPO lessonPO, Model model, HttpSession session) {
        Student user = (Student) session.getAttribute("user");
        if (lessonPO == null || lessonPO.getClassLessonIssueId() == null) {
            return R.FAILURE("请填写关键参数", null);
        }
        if(lessonPO.getQ() == null){
            lessonPO.setQ(100);
        }
        int i = studentService.handInLesson(lessonPO);
        if (i > 0) {
            return R.SUCCESS("", i);
        }
        return R.FAILURE("本期已结束。请参与下一期。", 0);
    }





}